<template>
  <el-card class="app-container" shadow="always">
    <div slot="header">
      <span>添加菜品预约</span>
    </div>
    <el-form ref="form1" :model="form1">
      <el-form-item
        :rules="[
          { required: true, message: '不能为空'}
        ]"
        label="菜品名称">
        <el-select v-model="form1.dishId" placeholder="请选择菜品">
          <el-option v-for="t in typeList" :label="t.dishName" :value="t.dishId" :key="t.dishId">
            {{ t.dishId + ' ' + t.dishName + '(' + t.dishPrice + '元)' }}
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        :rules="[
          { required: true, message: '不能为空'}
        ]"
        label="预约数量"
        prop="num"
      >
        <el-input v-model="form1.num" placeholder="预约数量"/>
      </el-form-item>

      <el-form-item
        :rules="[
          { required: true, message: '不能为空'}
        ]"
        label="预约人手机号"
        prop="phone"
      >
        <el-input v-model="form1.phone" placeholder="请输入手机号"/>
      </el-form-item>
      <el-form-item
        :rules="[
          { required: true, message: '不能为空'}
        ]"
        label="预约日期"
      >

        <el-date-picker
          v-model="form1.time"
          :picker-options="expireTimeOption"
          type="date"
          placeholder="选择日期"
          @change="calcDays"/>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" type="primary" @click="onSubmit()">提交</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { addDishes} from '@/api/dishes'
import { getAllDishesManager} from '@/api/dishManager'
import Cookies from 'js-cookie'

export default {
  name: 'Add',
  data() {
    return {
      form1: {
        dishId: '',
        num: '',
        time: '',
        userId: null,
        userName: null,
        phone: '',
        role: null
      },
      orderDateRange: null,
      textarea2: '',
      typeList: null,
      loading: false,
      roomTypeList: [],
      orderTypeList: [],
      expireTimeOption: {
        disabledDate: time => {
          return time.getTime() < Date.now() - 24 * 60 * 60 * 1000
        }
      }
    }
  },
  created: function() {
    this.form1.userName = Cookies.get('username')
    this.form1.userId = Cookies.get('user_id')
    this.form1.role = Cookies.get('role')
    this.fetchData()
  },
  methods: {
    fetchData() {
      getAllDishesManager().then(res => {
        this.typeList = res.data
      })
    },
    calcDays() {
      this.form1.orderDate = this.orderDateRange[0]
      var days = this.orderDateRange[1].getTime() - this.orderDateRange[0].getTime()
      this.form1.orderDays = days / (24 * 60 * 60 * 1000)
    },
    onSubmit() {
      this.$refs.form1.validate((valid) => {
        if (valid) {
          this.loading = true
          addDishes(this.form1).then(response => {
            const res = response
            if (res.code === 1000) {
              this.$message({
                message: '提交成功！',
                type: 'success'
              })
              this.loading = false
              setTimeout(this.onCancel(), 20000)
            } else {
              this.showError()
              this.loading = false
            }
          })
        } else {
          this.loading = false
          return false
        }
      })
    },
    showError() {
      this.$message({
        message: '提交失败！',
        type: 'error'
      })
    },
    onCancel() {
      this.$router.back()
    }
  }
}
</script>

<style scoped>

</style>
